/**************************************************************************//**
 * @item     CosyOS-III Kernel
 * @file     sv_task.h
 * @brief    任务服务
 * @details  仅在任务中调用并执行。
 * @author   迟凯峰
 * @version  V1.2.2
 * @date     2025.04.27
 ******************************************************************************/

#ifndef __SV_TASK_H
#define __SV_TASK_H

#include "os_var.h"

#if MCUCFG_ISA == __ARM__
/** 调度上锁（进入嵌套任务临界区）*/
__STATIC_FORCEINLINE void su_schedule_locks(void)
{
	s_sign_schedule.every[0] = true;
	s_schedulelock_counter++;
}

/** 内核上锁（进入非嵌套服务层临界区）*/
__STATIC_FORCEINLINE bool su_kernel_lock0(void)
{
	mSysIRQ_Disable();
    return true;
}

/** 内核上锁（进入嵌套服务层临界区）*/
#if SYSCFG_KERNELLOCKNESTING == __ENABLED__
__STATIC_FORCEINLINE bool su_kernel_locks(void)
{
	mSysIRQ_Disable();
	s_kernellock_counter++;
	return true;
}
#endif

#else
void       su_schedule_locks    (void);
void       su_kernel_lock0      (void);
#if SYSCFG_KERNELLOCKNESTING == __ENABLED__
void       su_kernel_locks      (void);
#endif
#endif

#if SYSCFG_KERNELLOCKNESTING == __DISABLED__
#define su_kernel_locks   su_kernel_lock0
#endif

void       su_schedule_unlocks  (void);
void       su_kernel_unlocks    (void);
void      *su_kernel_unlocks_ret(void *p);
void       su_kernel_unlock0_psv(void);

s_ecode_t  su_startup_task      (s_taskhand_tsp hhand, s_u8_t status);
s_ecode_t  su_resume_task       (s_tasknode_tsp htask);
s_ecode_t  su_resume_suspend    (s_tasknode_tsp htask);
s_ecode_t  su_suspend_task      (s_tasknode_tsp htask);
s_ecode_t  su_delete_task       (s_tasknode_tsp htask);
s_ecode_t  su_clear_block       (s_tasknode_tsp htask);
s_ecode_t  su_set_block         (s_tasknode_tsp htask, s_delay_t tick);
s_ecode_t  su_set_taskpri       (s_tasknode_tsp htask, s_u8_t npri);
void       su_yield_tasking     (void);
void       su_delay             (s_delay_t tick);
bool       su_take_mutex        (s_mutex_tsp hmut, s_delay_t tick);
void       su_back_mutex        (s_mutex_tsp hmut);
bool       su_wait_binary       (s_binary_tsp hbin, s_delay_t tick);
bool       su_take_binary       (s_binary_tsp hbin, s_delay_t tick);
bool       su_take_semaphore    (s_semaph_tsp hsem, s_delay_t tick);
m_fetion_t su_recv_fetion       (s_tionbox_tsp htbox, s_delay_t tick);
void      *su_recv_mail         (s_mailbox_tsp hmbox, s_delay_t tick);
void      *su_recv_msg          (s_queue_tsp hque, s_delay_t tick);
s_ecode_t  su_send_msg          (s_queue_tsp hque, void *msg);
bool       su_query_group       (void _STATIC_MEM_ *hgrp, s_u8_t size);
bool       su_wait_group_1      (s_u8_t _STATIC_MEM_ *hgrp, s_delay_t tick);
bool       su_wait_group_2      (s_u16_t _STATIC_MEM_ *hgrp, s_delay_t tick);
bool       su_wait_group_4      (s_u32_t _STATIC_MEM_ *hgrp, s_delay_t tick);
bool       su_init_mempool      (s_thrmem_tsp hmem, size_t size);
void _MALLOC_MEM_ *su_talloc    (s_thrmem_tsp hmem, size_t size);
void _MALLOC_MEM_ *su_xalloc    (void _MALLOC_MEM_ *p);



/**
 @addtogroup 任务服务
 @{
 */

/*
 * 操作任务
 */

/** 启动任务 */
#define sUSV_StartTask(hhand, status) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_startup_task((s_taskhand_tsp)hhand, status) : OS_ECODE_NOERROR \
)

/** 挂起任务 */
#define sUSV_SuspendTask(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_suspend_task(htask) : OS_ECODE_NOERROR \
)

/** 恢复任务 */
#define sUSV_ResumeTask(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_resume_task(htask) : OS_ECODE_NOERROR \
)

/** 恢复指定任务并挂起自身任务 */
#define sUSV_ResumeSuspend(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_resume_suspend(htask) : OS_ECODE_NOERROR \
)

/** 删除任务 */
#define sUSV_DeleteTask(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_delete_task(htask) : OS_ECODE_NOERROR \
)

/** 设置任务优先级 */
#define sUSV_SetTaskPri(htask, npri) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_set_taskpri(htask, npri) : OS_ECODE_NOERROR \
)

/** 设置阻塞（时间）*/
#define sUSV_SetBlock(htask, tick) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_set_block(htask, tick) : OS_ECODE_NOERROR \
)

/** 清除阻塞（状态）*/
#define sUSV_ClearBlock(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_clear_block(htask) : OS_ECODE_NOERROR \
)

/** 自身任务延时 */
#define sUSV_Delay(tick) \
do{ \
	mSysIRQ_Disable(); \
	su_delay(tick); \
}while(false)



/*
 * 定时
 */

/** 定时中断 */
#define sUSV_TimInt(tmid, tick) \
do{ \
	su_kernel_locks(); \
	sv_timint(tmid, tick); \
	su_kernel_unlocks(); \
}while(false)

/** 定时查询 */
#define sUSV_TimQry(tmid, tick) \
do{ \
	su_kernel_locks(); \
	sv_timqry(tmid, tick); \
	su_kernel_unlocks(); \
}while(false)



/*
 * 互斥信号量
 */

/** 获取互斥信号量 */
#define sUSV_TakeMut(mut, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_take_mutex(&mut, tick) : false \
)

/** 归还互斥信号量 */
#define sUSV_BackMut(mut) \
do{ \
	su_kernel_locks(); \
	su_back_mutex(&mut); \
}while(false)



/*
 * 二值信号量
 */

/** 等待二值信号量 */
#define sUSV_WaitBin(bin, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_wait_binary(&bin, tick) : false \
)

/** 获取二值信号量 */
#define sUSV_TakeBin(bin, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_take_binary(&bin, tick) : false \
)

/** 给予二值信号量 */
#define sUSV_GiveBin(bin) \
do{ \
	su_kernel_locks(); \
	sv_give_binary(&bin); \
	su_kernel_unlocks(); \
}while(false)



/*
 * 计数信号量
 */

/** 获取计数信号量 */
#define sUSV_TakeSem(sem, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_take_semaphore(&sem, tick) : false \
)

/** 给予计数信号量 */
#define sUSV_GiveSem(sem) \
do{ \
	su_kernel_locks(); \
	sv_give_semaphore(&sem); \
	su_kernel_unlocks(); \
}while(false)



/*
 * 飞信
 */

/** 接收飞信 */
#define sUSV_RecvFetion(tbox, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_recv_fetion(&tbox, tick) : false \
)

/** 发送飞信 */
#define sUSV_SendFetion(tbox, tion) \
do{ \
	su_kernel_locks(); \
	sv_send_fetion(&tbox, tion); \
	su_kernel_unlocks(); \
}while(false)



/*
 * 邮箱
 */

/** 接收邮件 */
#define sUSV_RecvMail(mbox, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_recv_mail(&mbox, tick) : OS_NULL \
)

/** 发送邮件 */
#define sUSV_SendMail(mbox, mail) \
do{ \
	su_kernel_locks(); \
	sv_send_mail(&mbox, mail); \
	su_kernel_unlocks(); \
}while(false)



/*
 * 消息队列
 */

/** 接收消息 */
#define sUSV_RecvMsg(que, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_recv_msg((s_queue_tsp)&que, tick) : OS_NULL \
)

/** 发送消息 */
#define sUSV_SendMsg(que, msg) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_send_msg((s_queue_tsp)&que, msg) : OS_ECODE_NOERROR \
)



/*
 * 事件标志组
 */

/** 查询标志组 */
#define sUSV_QueryFlagGroup(group) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_query_group((void *)&group, sizeof(group)) : false \
)

/** 等待标志组 */
#define sUSV_WaitFlagGroup(group, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? ( \
		  sizeof(group) == 1 ? su_wait_group_1((s_u8_t  *)&group, tick) \
		: sizeof(group) == 2 ? su_wait_group_2((s_u16_t *)&group, tick) \
		: sizeof(group) == 4 ? su_wait_group_4((s_u32_t *)&group, tick) \
		: false \
	) : false \
)

/** 清除标志组 */
#define sUSV_ClearFlagGroup(group) \
do{ \
	su_kernel_locks(); \
	sizeof(group) == 1 ? *(s_u8_t  *)&group = false : MCUCFG_TERNARYMASK \
	sizeof(group) == 2 ? *(s_u16_t *)&group = false : MCUCFG_TERNARYMASK \
	sizeof(group) == 4 ? *(s_u32_t *)&group = false : false; \
	su_kernel_unlocks(); \
}while(false)

/** 写多标志位 */
#define sUSV_WriteFlagBits(group, value, nbit) \
	suWriteFlagBits_##nbit(group, value, 



/*
 * 软件RTC
 */

/** 获取时间 */
#define sUSV_GetTime(t) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_ret(s_memcpy(t, &s_rtc, sizeof(s_rtc_ts))) : OS_NULL \
)

/** 设置时间 */
#if SYSCFG_GETRTC_INT == __ENABLED__
#define sUSV_SetTime(t) \
do{ \
	su_kernel_locks(); \
	s_memcpy(&s_rtc, t, sizeof(s_rtc_ts)); \
	s_sign_every.set = true; \
	sv_update_rtccopy(); \
	su_kernel_unlocks(); \
}while(false)
#else
#define sUSV_SetTime(t) \
do{ \
	su_kernel_locks(); \
	s_memcpy(&s_rtc, t, sizeof(s_rtc_ts)); \
	s_sign_every.set = true; \
	su_kernel_unlocks(); \
}while(false)
#endif



/*
 * 进程内存
 */

/** malloc */
#define sUSV_Malloc(size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_xalloc((void _MALLOC_MEM_ *)s_malloc(size)) : OS_NULL \
)

/** calloc */
#define sUSV_Calloc(nmemb, size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_xalloc((void _MALLOC_MEM_ *)s_calloc(nmemb, size)) : OS_NULL \
)

/** realloc */
#define sUSV_Realloc(p, size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_xalloc((void _MALLOC_MEM_ *)s_realloc(p, size)) : OS_NULL \
)

/** free */
#define sUSV_Free(p) \
do{ \
	su_kernel_locks(); \
	s_free(p); \
	su_kernel_unlocks(); \
}while(false)



/*
 * 线程内存
 */

/** 初始化线程内存池 */
#define sUSV_InitMempool(size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_init_mempool(&u_thrmem, size) : false \
)

/** 线程内存分配 */
#define sUSV_Talloc(size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_xalloc(su_talloc(&u_thrmem, size)) : OS_NULL \
)

/** 释放线程内存池 */
#define sUSV_FreeMempool() \
do{ \
	sUSV_Free(u_thrmem.head); \
	u_thrmem.head = u_thrmem.move = OS_NULL; \
}while(false)

/** @} */

#endif
